
using System;
using System.Collections.Generic;
using System.Linq;
using abilities.templates;
using abilities.triggers.conditions;
using LitJson;
using UnityEngine;

namespace abilities.triggers
{
    //触发器的模板，从json中提取
    public class AbilityTriggerTemplate
    {
        public bool startTrigger { get;private set; }
        public AbilityTriggerType type { get;private set; }
        //重复多少次
        public int repeat { get;private set; }
        // 需要执行多少次(所有条件检查通过)才能生效
        public int countBeforeActivate { get;private set; }
        public List<int> interval { get; private set; } = new();
        public int delay  { get;private set; }
        public int minRadius  { get;private set; }
        public int maxRadius  { get;private set; }
        public int articleId  { get;private set; }
        public List<AbilityTriggerCondition> conditions{get; private set; }

        static List<string> validTriigerType = Enum.GetNames(typeof(AbilityTriggerType)).ToList();
        public static AbilityTriggerTemplate TriggerFromJson(JsonData value, bool isStartTrigger)
        {
            if (value.GetJsonType() == JsonType.None)
                return null;

            AbilityTriggerTemplate result = null;

            string type = (string)value["type"];
            if (validTriigerType.Contains(type))
            {
                result = new AbilityTriggerTemplate();
                result.FromJson(value, isStartTrigger);
            }
            else
            {
                Debug.Log("Unknown trigger type");
            }
            return result;
        }


        public virtual bool FromJson(JsonData value, bool isStartTrigger)
        {
            startTrigger = isStartTrigger;
            type = Enum.Parse<AbilityTriggerType>((string)value["type"]);
            repeat = value.ContainsKey("repeat") ? (int)value["repeat"] : -1;
            countBeforeActivate = (int)value["countBeforeActivate"];
            EffectTemplate.FromJsonToArray(value["interval"], interval);
            delay = (int)(value["delay"]);
            articleId = (int)value["articleId"];
            minRadius = value.ContainsKey("minRadius") ? (int)value["minRadius"] : -1;
            maxRadius = value.ContainsKey("maxRadius") ? (int)value["maxRadius"] : -1;

            conditions = new();
            if (value.ContainsKey("conditions"))
            {
                foreach (JsonData i in value["conditions"])
                {
                    AbilityTriggerCondition condition = AbilityTriggerCondition.CreateFromJson(i);
                    if (condition != null)
                    {
                        conditions.Add(condition);
                    }
                }
            }

            return true;
        }


        public AbilityTrigger CreateInstance(int level)
        {
            AbilityTrigger result = null;
            if (type == AbilityTriggerType.Default)
                result = new AbilityTriggerDefault();
            else if (type == AbilityTriggerType.Timer)
                result = new AbilityTriggerTimer();
            else if (type == AbilityTriggerType.OnHit)
                result = new AbilityTriggerOnHit();
            else if (type == AbilityTriggerType.OnHelpChange)
                result = new AbilityTriggerOnHealthChanged();
            else if (type == AbilityTriggerType.OnAttack)
                result = new AbilityTriggerOnAttack();
            else if (type == AbilityTriggerType.OnDeath)
                result = new AbilityTriggerOnDeath();
            else if (type == AbilityTriggerType.Distanceto)
                result = new AbilityTriggerDistanceTo();

            if (result != null) result.InitInernal(this, level);
            return result;
        }


        public int GetInterval(int level)
        {
            return EffectTemplate.GetByLevel(interval, level);
        }

    }
}